package cn.ffcs.organ.web;

import java.util.Iterator;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import cn.ffcs.base.web.BaseAction;
import cn.ffcs.base.web.PageControl;
import cn.ffcs.organ.model.Person;
import cn.ffcs.organ.service.IPersonService;
import cn.ffcs.util.ext.Expression;

/**
 * 人员action类<br/>
 * <table>
 * <tr>
 * <td>负责人/principal:</td>
 * <td colspan="2">liuzhh</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author liuzhh
 * @version $Id$
 */
public class PersonAction<P extends Person> extends BaseAction<P>
{

    /** 业务POJO **/
    private P person;
    /**服务 **/
    private IPersonService personService;
    /**组织ID **/
    private int organId;
    /**
     * 新增或修改字典类型数据
     * @return
     */
    public String saveOrUpdate()
    {
        baseService.saveOrUpdate(person);
        returnMsg="保存操作已成功！";
        return SUCCESS;
    }
    /**
     * 查询未跟当前部门关联的所有员工
     * @return
     */
    public String findSelectByPage(){
        if (pageControl == null)
        {
            pageControl = new PageControl();
        }
        pageControl = personService.findSelectByPage(person, pageControl.getPageNo(),
                pageControl.getPageSize(),organId);
        return SUCCESS;
    }
    
    /**
     * 获取分页数据
     * @return
     */
    @SuppressWarnings("unchecked")
    public String findByPage()
    {
              
        //如果第一次装置，默认参数
        if (pageControl == null)
        {
            pageControl = new PageControl();
        }
        
        pageControl = baseService.findByPage(combineCondExp(), pageControl.getPageNo(),
                pageControl.getPageSize());
        return SUCCESS;
    }
    
    /**
     * 
     * 组合查询条件
     * @return
     */
    protected Criterion combineCond(){
        Criterion cond = null;
        if (super.getConds() != null)
        {
            //遍历参数表
            Iterator i = super.getConds().entrySet().iterator();
            while (i.hasNext())
            {
                Entry entry = (Entry)i.next();
                String key=(String)entry.getKey();
                if (super.getConds().get(key) != null
                        && !StringUtils.isEmpty(super.getConds().get(key).toString()))
                {
                    //根据实际情况做处理
                    Criterion temp=null;
                    if (key.equalsIgnoreCase("name")){
                        temp=Restrictions.like(key, "%"+super.getConds().get(key).toString()+"%");
                    }else{
                        temp=Restrictions.eq(key, super.getConds().get(key));                        
                    }
                    
                    if (cond == null)
                    {
                        cond = temp;
                    }
                    else
                    {
                        cond = Restrictions.and(cond, temp);
                    }
                }
            }
        }
        return cond;
    }
    
    protected Expression combineCondExp(){
        Expression exp=new Expression();
        if (super.getConds() != null)
        {
            //遍历参数表
            Iterator i = super.getConds().entrySet().iterator();
            while (i.hasNext()) 
            {
                Entry entry = (Entry)i.next();
                String key=(String)entry.getKey();
                if (super.getConds().get(key) != null
                        && !StringUtils.isEmpty(super.getConds().get(key).toString()))
                {
                    //根据实际情况做处理
                    Expression temp=new Expression();
                    if (key.equalsIgnoreCase("name")){
                        temp.like(key, super.getConds().get(key).toString());
                    }else{
                        temp.eq(key, super.getConds().get(key));                        
                    }
                    
                    if (exp == null || exp.getSqlString()==null)
                    {
                        exp = temp;
                    }
                    else
                    {
                        exp.and(exp, temp);
                    }
                }
            }
        }
        return exp;
    } 
    /**
     * 删除数据
     * @return
     */
    public String remove()
    {
        baseService.remove(person);
        returnMsg="删除操作已成功！";
        return SUCCESS;
    }

    /**
     * 批量删除
     * @return
     */
    public String batchDel()
    {
        String[] ids = getContextObjects("ids");
        baseService.batchDel(ids);
        returnMsg="批量删除操作已成功！";
        return SUCCESS;
    }

    /**
     * 根据关键值获取实体数据
     * @return
     */
    public String findById()
    {
        if (person != null && person.getId() != 0)
        {
            person = (P) baseService.findById(person.getId());
        }     
        
        return SUCCESS;
    }
    
    public P getPerson()
    {
        return person;
    }

    public void setPerson(P person)
    {
        this.person = person;
    }
    public int getOrganId()
    {
        return organId;
    }
    public void setOrganId(int organId)
    {
        this.organId = organId;
    }
    public IPersonService getPersonService()
    {
        return personService;
    }
    public void setPersonService(IPersonService personService)
    {
        this.personService = personService;
    }

}
